<?php
class Jurnal {	
	public static function createHeader($notes,$jurnalDate,$jurnalType="Auto"){
		//Get Tahun Buku berdasarkan jurnalDate;
		$tahunBuku = MTahunBuku::model()->find("start_date <= :jurnalDate and end_date >= :jurnalDate" , array(":jurnalDate"=>$jurnalDate));
		if ($tahunBuku == null){
			throw new Exception("No Tahun buku Registered for this date $jurnalDate"); 		
		}else{
			if ($tahunBuku->is_close){
				throw new Exception("Tahun buku is closed for this date $jurnalDate");
			}
		}
		$hJurnal = new MHjurnal();
		$hJurnal->tahun_buku_id =$tahunBuku->id;
		$hJurnal->no_jurnal = AutoNumber::getJurnalNo();
		$hJurnal->notes = $notes;
		$hJurnal->jurnal_date = $jurnalDate;
		$hJurnal->input_date_time = date("Y-m-d");
		$hJurnal->jurnal_type = $jurnalType;
		$hJurnal->is_active = 1;
		if (!$hJurnal->save()){
			throw new Exception(json_encode($hJurnal->getErrors()));
		}
		
		return $hJurnal;
	}
	
	public static function createDetail($jurnalId,$coaId,$notes,$pos,$total){
		$result = array();
		if ($total == 0){
			return null;
		}
		$dJurnal = new MDjurnal();
		$dJurnal->jurnal_id = $jurnalId;
		$dJurnal->coa_id = $coaId;
		$dJurnal->notes= $notes;
		$dJurnal->position = $pos;
		$dJurnal->total = $total;
		if (!$dJurnal->save()){
			throw new Exception(json_encode($dJurnal->getErrors()));
		}
		
		return $dJurnal;		
	}
	
	public static function cancelJurnal($jurnalId){
		/*
		 * lakukan pembalikan jurnal
		 */		
		$hJurnal = MHjurnal::model()->find("id =:jurnalId",array(":jurnalId"=>$jurnalId));
		
		$resp = Jurnal::createHeader("Cancel - " .$hJurnal->no_jurnal, date("Y-m-d"),null,"Auto");
		$newId =$resp["id"]; 
		if ($newId == 0){
			return $resp;
		}			
		$arrDJurnal = MDjurnal::model()->findAll("juranl_id = :jurnalId",array(":jurnalId"=>$jurnalId));
		foreach ($arrDJurnal as $dJurnal){
			if ($dJurnal->position = "D"){
				$newPos = "C";
			}else{
				$newPos = "D";
			}
			$resp = Jurnal::createDetail($newId, $dJurnal->coa_id, "", $newPos, $dJurnal->total);
			$tmpId =$resp["id"];
			if ($tmpId == 0){
				return $resp;
			}	
		}
		
		$result["id"] = $newId;
		$result["message"] = "";		
		return $result;
	}
	
	
	public static function deleteDetailJurnal($jurnalId,$conn=null){
		$hJurnal = MHjurnal::model()->findByPK($jurnalId);
		if ($hJurnal == null){
			throw new Exception("Invalid Jurnal Id");
		} 
		$tahunBuku = MTahunBuku::model()->findByPK($hJurnal->tahun_buku_id);
		if ($tahunBuku == null){
			throw new Exception("No Tahun buku Registered for this date " .$hJurnal->jurnal_date);
		}else{
			if ($tahunBuku->is_close){
				throw new Exception("Tahun buku is closed for this date ". $hJurnal->jurnal_date);
			}
		}
		if ($conn == null){
			$conn = Yii::app()->db;
		}
		$ssql = "delete from m_djurnal where jurnal_id =:jurnalId";
		$cmd = $conn->createCommand($ssql);
		//throw new Exception('Jurnal period is closing');		
		return $cmd->execute(array(":jurnalId"=>$jurnalId));
	}
	
	public static function deleteHeaderJurnal($jurnalId,$conn=null){
		$hJurnal = MHjurnal::model()->findByPK($jurnalId);
		if ($hJurnal == null){
			throw new Exception("Invalid Jurnal Id");
		}
		$tahunBuku = MTahunBuku::model()->findByPK($hJurnal->tahun_buku_id);
		if ($tahunBuku == null){
			throw new Exception("No Tahun buku Registered for this date " .$hJurnal->jurnal_date);
		}else{
			if ($tahunBuku->is_close){
				throw new Exception("Tahun buku is closed for this date ". $hJurnal->jurnal_date);
			}
		}
		
		if ($conn == null){
			$conn = Yii::app()->db;
		}
		$ssql = "delete from m_hjurnal where id =:jurnalId";
		$cmd = $conn->createCommand($ssql);
		//throw new Exception('Jurnal period is closing');
		return $cmd->execute(array(":jurnalId"=>$jurnalId));
	}
	
	
}